package com.finance.rule.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.finance.base.enums.*;
import com.finance.base.exception.BaseException;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.rule.bean.pojo.PieceRangeRule;
import com.finance.rule.dao.PieceRangeMapper;
import com.finance.system.bean.pojo.Region;
import com.finance.system.bean.pojo.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("pieceRangeService")
public class PieceRangeService implements IPieceRangeService {

	@Autowired
	private PieceRangeMapper pieceRangeMapper;

	private ObjectMapper objectMapper = new ObjectMapper();

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_'+ '#id'",condition = "#result != null")
	public PieceRangeRule getById(Long id) throws BaseException {
		return this.pieceRangeMapper.getById(id);
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "piecerule__all",condition = "#result != null")
	public List<PieceRangeRule> listAll(){
		return pieceRangeMapper.listAll();
	}

	@Override
	public PageResultBO<PieceRangeRule> listByPage(Page page) {
		PageResultBO<PieceRangeRule> resultBO = new PageResultBO<PieceRangeRule>();
		List<PieceRangeRule> list = this.pieceRangeMapper.listByPage(page);
		resultBO.setDataList(list);
		resultBO.setPageSize(page.getPageSize());
		resultBO.setPageNo(page.getPageNo());
		resultBO.setTotalRecord(page.getTotalRecord());
		return resultBO;
	}

	@Override
	@CacheEvict(value="rule",cacheResolver = "ruleCacheResolver",allEntries = true)
	public boolean add(PieceRangeRule pieceRange) {
		boolean result = this.pieceRangeMapper.add(pieceRange) == 1;
		return result;
	}

	@Override
	@CacheEvict(value="rule",cacheResolver = "ruleCacheResolver",allEntries = true)
	public boolean delete(String uId,List<String> ids) {
		boolean result = this.pieceRangeMapper.delete(uId,ids) > 0;
		return result;
	}

	@Override
	@CacheEvict(value="rule",cacheResolver = "ruleCacheResolver",allEntries = true)
	public boolean update(PieceRangeRule pieceRange) throws BaseException {
		boolean result = pieceRangeMapper.update(pieceRange) == 1;
		return result;
	}

	@Override
	@CacheEvict(value="rule",cacheResolver = "ruleCacheResolver",allEntries = true)
	public boolean batchAdd(List<PieceRangeRule> pieceRanges){
		boolean result = pieceRangeMapper.batchAdd(pieceRanges) > 0;
		return result;
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_supplier_'+ '#supplier.code'",condition = "#result != null")
	public List<PieceRangeRule> getPieceRangeRuleBySupplier(Supplier supplier) {
		return this.pieceRangeMapper.getPieceRangeRuleBySupplier(supplier);
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_region_'+ '#region.code'",condition = "#result != null")
	public List<PieceRangeRule> getPieceRangeRuleByRegion(Region region) {
		return this.pieceRangeMapper.getPieceRangeRuleByRegion(region);
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_direction_'+ '#directionEnum.value'",condition = "#result != null")
	public List<PieceRangeRule> getPieceRangeRuleByDirection(DirectionEnum directionEnum) {
		return this.pieceRangeMapper.getPieceRangeRuleByDirection(directionEnum.getValue());
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_temperature_'+ '#temperatureEnum.value'",condition = "#result != null")
	public List<PieceRangeRule> getPieceRangeRuleByTemperature(TemperatureEnum temperatureEnum) {
		return this.pieceRangeMapper.getPieceRangeRuleByTemperature(temperatureEnum.getValue());
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_scheduleType_'+ '#expressScheduleTypeEnum.value'",condition = "#result != null")
	public List<PieceRangeRule> getPieceRangeRuleByScheduleType(ExpressScheduleTypeEnum expressScheduleTypeEnum) {
		return this.pieceRangeMapper.getPieceRangeRuleByScheduleType(expressScheduleTypeEnum.getValue());
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_packageSize_'+ '#packageSizeEnum.value'",condition = "#result != null")
	public List<PieceRangeRule> getPieceRangeRuleByPackageSize(PackageSizeEnum packageSizeEnum) {
		return this.pieceRangeMapper.getPieceRangeRuleByPackageSize(packageSizeEnum.getValue());
	}

	@Override
	@Cacheable(value = "rule",cacheResolver = "ruleCacheResolver", key = "'piecerule_signer_'+ '#signerEnum.value'",condition = "#result != null")
	public List<PieceRangeRule> getPieceRangeRuleBySinger(SignerEnum signerEnum) {
		return this.pieceRangeMapper.getPieceRangeRuleBySinger(signerEnum.getValue());
	}
}
